﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JMT.Data
{
    public class ModelNameCreator
    {
        private Dictionary<Type, int> _seeds = new Dictionary<Type, int>();

        public Dictionary<Type, int> Seeds
        {
            get
            {
                return _seeds;
            }
        }

        public string GetNewName<T>() where T : JModel
        {
            Type type = typeof(T);
            return GetNewName(type, JConstString.GetType<T>());
        }

        public string GetNewName(string type)
        {
            Type modelType = null;
            if (type == JConstString.TSolution)
            {
                modelType = typeof(JSolution);
            }
            else if (type == JConstString.TPackage)
            {
                modelType = typeof(JPackage);
            }
            else if (type == JConstString.TDiagram)
            {
                modelType = typeof(JDiagram);
            }
            else if (type == JConstString.TInterface)
            {
                modelType = typeof(JInterface);
            }
            else if (type == JConstString.TClass)
            {
                modelType = typeof(JClass);
            }
            else if (type == JConstString.TMethod)
            {
                modelType = typeof(JMethod);
            }
            else if (type == JConstString.TProperty)
            {
                modelType = typeof(JProperty);
            }
            else if (type == JConstString.TElement)
            {
                modelType = typeof(JElement);
            }
            else if (type == JConstString.TGeneralization)
            {
                modelType = typeof(JGeneralization);
            }
            else if (type == JConstString.TAggregation)
            {
                modelType = typeof(JAggregation);
            }
            else if (type == JConstString.TAssociation)
            {
                modelType = typeof(JAssociation);
            }
            else if (type == JConstString.TComposition)
            {
                modelType = typeof(JComposition);
            }
            return GetNewName(modelType, type);
        }

        private string GetNewName(Type type, string typeName)
        {
            int index = 1;
            if (_seeds.ContainsKey(type))
            {
                index = _seeds[type];
                _seeds[type]++;
            }
            else
            {
                _seeds.Add(type, 2);
            }
            return string.Format("{0}{1}", typeName, index);
        }
    }
}
